package com.kogi.androidpatt.dealing.draganddrop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import com.kogi.androidpatt.dealing.FileLoader;
import com.kogi.androidpatt.dealing.draganddrop.customlist.DragListener;
import com.kogi.androidpatt.dealing.draganddrop.customlist.DragNDropAdapter;
import com.kogi.androidpatt.dealing.draganddrop.customlist.DragNDropListView;
import com.kogi.androidpatt.dealing.draganddrop.customlist.DropListener;
import com.kogi.androidpatt.dealing.draganddrop.customlist.RemoveListener;
import com.kogi.patterns.R;
import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;



public class DragAndDrop extends ListActivity {
	
	
	
	private FileLoader data;
	
	//private ArrayList<String> array=new ArrayList<String>(Arrays.asList(mListContent));
	
	@Override
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	       
	        setContentView(R.layout.main);
	        
	        data = new FileLoader(this);
			data.readFileLatitude();
	 	    
	        ArrayList<String> content = new ArrayList<String>();//(mListContent.length);
	        ArrayList<String> value = new ArrayList<String>();
	        
	        content = data.getNames();
	       
	        Collections.shuffle(content);
	       // for (int i=0; i < mListContent.length; i++) {
	        //	content.add(mListContent[i]);
	       // }
	        
	     
	        setListAdapter(new DragNDropAdapter(this, new int[]{R.layout.custom_row_drag_and_drop}, new int[]{R.id.textViewdrangAnddrop,R.id.customDragRowLayout}, content));//new DragNDropAdapter(this,content)
	      
	        ListView listView =  getListView();
	        
	       
	        
	        if (listView instanceof DragNDropListView) {
	        	((DragNDropListView) listView).setDropListener(mDropListener);
	        	((DragNDropListView) listView).setRemoveListener(mRemoveListener);
	        	((DragNDropListView) listView).setDragListener(mDragListener);
	        }
	       
	}
	
	
	
	private DropListener mDropListener = 
		new DropListener() {
        public void onDrop(int from, int to) {
        	ListAdapter adapter = getListAdapter();
        	if (adapter instanceof DragNDropAdapter) {
        		((DragNDropAdapter)adapter).onDrop(from, to);
        		getListView().invalidateViews();
        	}
        }
    };
    
    private RemoveListener mRemoveListener =
        new RemoveListener() {
        public void onRemove(int which) {
        	ListAdapter adapter = getListAdapter();
        	if (adapter instanceof DragNDropAdapter) {
        		((DragNDropAdapter)adapter).onRemove(which);
        		getListView().invalidateViews();
        	}
        }
    };
    
    private DragListener mDragListener =
    	new DragListener() {

    	int backgroundColor = 0xe0103010;
    	int defaultBackgroundColor;
    	
			public void onDrag(int x, int y, ListView listView) {
				// TODO Auto-generated method stub
			}

			public void onStartDrag(View itemView) {
				itemView.setVisibility(View.INVISIBLE);
				defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
				//itemView.setBackgroundColor(Color.parseColor("#00000000"));
				ImageView iv = (ImageView)itemView.findViewById(R.id.imageViewDragAndDrop);
				if (iv != null) iv.setVisibility(View.INVISIBLE);
			}

			public void onStopDrag(View itemView) {
				itemView.setVisibility(View.VISIBLE);
				//itemView.setBackgroundColor(Color.GREEN);//defaultBackgroundColor);
				ImageView iv = (ImageView)itemView.findViewById(R.id.imageViewDragAndDrop);
				if (iv != null) iv.setVisibility(View.VISIBLE);
			}
    	
    };
	
	

}
